package com.example.controller;

import com.example.common.AuthAccess;
import com.example.common.Result;
import com.example.pojo.entity.User;
import com.example.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
public class LoginController {

    @Resource
    UserService userService;

    /**
     * 默认请求接口
     */
    @GetMapping("/")
    public Result hello() {
        return Result.success("success");
    }

    @PostMapping("/login")
    @AuthAccess
    public Result login(@RequestBody User user) {
        if (user.getUsername() == null || user.getPassword() == null){
            return Result.error("输入不合法");
        }
        user = userService.login(user);
        return Result.success(user);
    }

    //注册
    @PostMapping("/register")
    @AuthAccess
    public Result register(@RequestBody User user) {
        if (user.getUsername() == null || user.getPassword() == null){
            return Result.error("输入不合法");
        }
        user = userService.register(user);
        return Result.success();
    }

    //重置密码
    @AuthAccess
    @PutMapping("/password")
    public Result password(@RequestBody User user) {
        if (user.getUsername() == null || user.getPhone() == null){
            return Result.error("输入不合法");
        }
        userService.resetPassword(user);
       return Result.success();
    }


}
